﻿#include "redis_query_task.h"
#include "redis_manager.h"
#include "redis/redis_db_connection.h"
#include "log.h"

CRedisDbQueryTask::CRedisDbQueryTask(const std::string &dbName, const std::string &collectionName, const std::string &queryString, const std::string &fieldsString)
	: m_dbName(dbName)
	, m_collectionName(collectionName)
	, m_queryString(queryString)
	, m_fieldsString(fieldsString)
{
	m_taskInstName = "CRedisDbQueryTask";
}

CRedisDbQueryTask::~CRedisDbQueryTask()
{

}

void CRedisDbQueryTask::Process(void *sharedObj)
{
	if (sharedObj == nullptr)
	{
		LOG_ERROR_TO("CRedisDbQueryTask", Fmt("task process failure, no find redisdb obj"));
		return;
	}
	CRedisDbConnection * pConn = (CRedisDbConnection *)sharedObj;
	if (nullptr != pConn)
	{
		m_resultSptr = pConn->Query(m_queryString );
	}
}

void CRedisDbQueryTask::OnCompleted()
{
	if (m_cb)
	{
		m_cb(m_resultSptr);
	}
}

void *CRedisDbQueryTask::CreateSharedObj(unsigned index)
{
	auto &objs = CRedisDbManager::get_mutable_instance().GetDbInstance(m_instName + "_query");
	if (index >= objs.size())
	{
		return nullptr;
	}
	return objs[index].get();
}